package ru.sviridov.sellandbuy;

import ru.sviridov.sellandbuy.model.Response;
import ru.sviridov.sellandbuy.model.query.QueryCategories;
import ru.sviridov.sellandbuy.tasks.base.BaseTask;
import ru.sviridov.sellandbuy.tasks.base.TaskRecipient;
import ru.sviridov.sellandbuy.url.APIGroup;
import ru.sviridov.sellandbuy.url.APIMethod;
import ru.sviridov.sellandbuy.url.URL;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import api.istsoft.ru.sellandbuy.dal.Response.Category;

public class ChooseCategoryActivity extends Activity implements TaskRecipient {
	public static final String CATEGORY_ID = "CATEGORY_ID";
	public static final String CATEGORY_NAME = "CATEGORY_NAME";
	
	private BaseTask<Void> task;
	private Category category;
	private Category[] categories;
	
	private Spinner spMain;
	private ViewGroup childrenViews;
	
	private Spinner.OnItemSelectedListener listener = new Spinner.OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
			Integer pos = (Integer)parent.getTag();
			for(int size=childrenViews.getChildCount(), i = size-1;i>pos;i--)
				childrenViews.removeViewAt(i);
			
			category = (Category)parent.getItemAtPosition(position);
			Category[] chCategories = category.getChildren();
			if(chCategories==null) return;
			
			Spinner newSpinner = new Spinner(ChooseCategoryActivity.this);
			newSpinner.setAdapter(new ArrayAdapter<Category>(ChooseCategoryActivity.this, android.R.layout.simple_list_item_1, chCategories));
			newSpinner.setOnItemSelectedListener(listener);
			newSpinner.setTag(pos+1);
			childrenViews.addView(newSpinner);
		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ContentViewMapper.setContentView(this);
		
		childrenViews = (ViewGroup) findViewById(R.id.llChirldren);
		spMain = (Spinner) findViewById(R.id.spinner_category);
		spMain.setTag(0);
		loadCategories();
		
		spMain.setOnItemSelectedListener(listener);

	}
	
	public void onClickFinish(View view){
		Intent data = new Intent();
		data.putExtra(CATEGORY_ID, category.getId());
		data.putExtra(CATEGORY_NAME, category.getName());
		setResult(RESULT_OK, data);
		finish();
	}
	
	private void loadCategories(){
		QueryCategories query = new QueryCategories();
		query.setCode(1);
		query.setParent(0L);
		
		task = new BaseTask<Void>(this, this);
		task.setQueryParams(query);
		task.execute(URL.getURL(APIGroup.SELLANDBUY, APIMethod.GET_CATEGORIES));
	}
	
	@Override
	public void onURLError(Context context, int errorCode) {
		if(context!=null)
			Toast.makeText(context, "Can't reach server. Error Code:"+errorCode, Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onJSONError(Context context, Response response) {
		if(context!=null)
			Toast.makeText(context, response.getErrorMessage(), Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onSuccess(Context context, Response response) {
		Object[] results = response.getResults();
		categories = new Category[results.length];
		for(int i =0;i<results.length;i++)
			categories[i] = (Category) results[i];
		spMain.setAdapter(new ArrayAdapter<Category>(this, android.R.layout.simple_list_item_1, categories));
	}
}
